package com.mall.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.result.Result;
import com.mall.system.entity.Notice;
import com.mall.system.service.NoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通知公告控制器
 */
@Tag(name = "通知公告管理")
@Slf4j
@RestController
@RequestMapping("/system/notices")
public class NoticeController {
    
    @Autowired
    private NoticeService noticeService;
    
    @Operation(summary = "通知公告列表")
    @GetMapping
    public Result<IPage<Notice>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Notice notice) {
        try {
            IPage<Notice> page = new Page<>(pageNum, pageSize);
            QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
            
            // 添加查询条件
            if (notice.getNoticeTitle() != null && !notice.getNoticeTitle().isEmpty()) {
                queryWrapper.like("notice_title", notice.getNoticeTitle());
            }
            if (notice.getNoticeType() != null) {
                queryWrapper.eq("notice_type", notice.getNoticeType());
            }
            if (notice.getStatus() != null) {
                queryWrapper.eq("status", notice.getStatus());
            }
            
            queryWrapper.orderByDesc("create_time");
            
            IPage<Notice> result = noticeService.page(page, queryWrapper);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询通知公告列表异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "根据通知公告ID获取通知公告")
    @GetMapping("/{noticeId}")
    public Result<Notice> getById(@PathVariable("noticeId") Long noticeId) {
        try {
            Notice notice = noticeService.getById(noticeId);
            if (notice != null) {
                return Result.success(notice);
            } else {
                return Result.failed("通知公告不存在");
            }
        } catch (Exception e) {
            log.error("查询通知公告异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "新增通知公告")
    @PostMapping
    public Result<Boolean> add(@RequestBody Notice notice) {
        try {
            boolean success = noticeService.save(notice);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("新增通知公告失败");
            }
        } catch (Exception e) {
            log.error("新增通知公告异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "更新通知公告")
    @PutMapping
    public Result<Boolean> update(@RequestBody Notice notice) {
        try {
            if (notice.getNoticeId() == null) {
                return Result.failed("通知公告ID不能为空");
            }
            
            boolean success = noticeService.updateById(notice);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("更新通知公告失败");
            }
        } catch (Exception e) {
            log.error("更新通知公告异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "删除通知公告")
    @DeleteMapping("/{ids}")
    public Result<Boolean> delete(@PathVariable("ids") List<Long> ids) {
        try {
            boolean success = noticeService.removeByIds(ids);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("删除通知公告失败");
            }
        } catch (Exception e) {
            log.error("删除通知公告异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "获取前台显示的通知公告列表")
    @GetMapping("/front")
    public Result<List<Notice>> getFrontNotices() {
        try {
            QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", 0); // 正常状态
            queryWrapper.orderByDesc("create_time");
            queryWrapper.last("LIMIT 5"); // 只取前5条
            
            List<Notice> notices = noticeService.list(queryWrapper);
            return Result.success(notices);
        } catch (Exception e) {
            log.error("获取前台显示的通知公告列表异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
}